package com.controller;

import com.request.GirlRequest;
import io.swagger.annotations.Api;
import com.model.Girl;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.repository.GirlRepository;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
@Api(value = "少女",tags = "少女girl")
public class GirlController {
    @Autowired
    private GirlRepository girlRepository;

    @PostMapping("/add")
    @ApiOperation("添加")
    public Girl add(@RequestBody Girl girl) {
        return girlRepository.save(girl);
    }

    @GetMapping("/getOne")
    @ApiOperation("一个")
    public Girl getOne(int id) {
        return girlRepository.queryGirlById(id);
    }

    @GetMapping("/getAlls")
    @ApiOperation("一群")
    public Iterable<Girl> getAll() {
        Iterable<Girl> iterator = girlRepository.findAll();
        return iterator;
    }

    @PostMapping("/query")
    @ApiOperation("查询")
    public Iterable<Girl> query(@RequestBody GirlRequest request) {
        QueryBuilder queryBuilder = QueryBuilders.termQuery("name", request.getName());
        return girlRepository.search(queryBuilder);
    }
}